<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      ref="crud"
      v-model="form"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot-scope="{ row }" slot="taskOutput">
        <a style="color: #4390ee; cursor: pointer" v-if="row.status === 3" @click="downloadFile(row.taskName, JSON.parse(row.taskOutput)[0].link)">点击下载</a>
        <a v-else> {{ row.taskOutput || '--' }} </a>
      </template>
      <!--      <template slot="menuRight">-->
      <!--        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>-->
      <!--      </template>-->
    </avue-crud>
  </basic-container>
</template>

<script>
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
import { getList, downLoadFile } from '@/api/admin/downloadCenter';
export default {
  name: 'downloadCenter',
  components: {
    userTableConfig
  },
  mixins: [userConfig()],
  mounted() {
    this.$nextTick(() => {
      //搜索与表格结构分隔
      this.$structuralSplit();
    });
  },
  data() {
    return {
      pageCode: 'DOWNLOAD_CENTER',
      data: [],
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      option: {
        height: 'calc(100vh - 360px)',
        calcHeight: 30,
        dialogWidth: 950,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: '序号',
        menu: false,
        addBtn: false,
        columnBtn: false,
        column: [
          {
            label: '任务名称',
            prop: 'taskName',
            width: 120,
            search: true
          },
          {
            label: '文件名称',
            prop: 'fileName',
            width: 120,
            search: true
          },
          {
            label: '时间',
            prop: 'createTime',
            width: 180
          },
          {
            label: '任务状态',
            prop: 'status',
            width: 100,
            html: true,
            formatter: (val, value, label) => {
              switch (value) {
                case 1:
                  return '<span class="text-green">' + '新建' + '</span>';
                  break;
                case 2:
                  return '<span class="text-green">' + '运行' + '</span>';
                  break;
                case 3:
                  return '<span class="text-red">' + '结束' + '</span>';
                  break;
                case 4:
                  return '<span class="text-yellow">' + '取消' + '</span>';
                  break;
                default:
                  return '<span class="text-red">' + '结束' + '</span>';
                  break;
              }
            }
          },
          {
            label: '操作人',
            prop: 'createUserName',
            width: 180
          },
          {
            label: '下载链接',
            prop: 'taskOutput',
            // html: true,
            slot: true
            // formatter: (val, value, label) => {
            //   try {
            //     let link = JSON.parse(value);
            //     if (link && link.length > 0 && link[0].link) {
            //       return `<a  href="${link[0].link}" download='${val.taskName}.xslx' style="color:#4390ee;cursor: pointer" >点击下载</a>`;
            //     } else {
            //       return '-';
            //     }
            //   } catch (e) {
            //     return value || '-';
            //   }
            // }
          }
        ]
      }
    };
  },
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
    async downloadFile(fileName, link) {
      try {
        const res = await downLoadFile({ link: link });
        const blob = new Blob([res.data]);
        const element = document.createElement('a');
        element.download = `${fileName}.xlsx`;
        element.style.display = 'none';
        element.href = URL.createObjectURL(blob);
        document.body.append(element);
        element.click();
        URL.revokeObjectURL(element.href);
        document.body.removeChild(element);
      } catch (e) {
        console.warn(e);
      }
    }
  }
};
</script>

<style scoped></style>
